//
//  MJRefreshBackGifFooter.swift
//  OC-Swift
//
//  Created by 陆文宇 on 2017/2/15.
//  Copyright © 2017年 陆文宇. All rights reserved.
//

import UIKit

class MJRefreshBackGifFooter: MJRefreshBackStateFooter {
    
    lazy var gifView: UIImageView = UIImageView()
    lazy var stateImages: Dictionary<MJRefreshState, Array<UIImage>> = [:]
    lazy var stateDurations: Dictionary<MJRefreshState, TimeInterval> = [:]
    override var pullingPercent: CGFloat? {
        set {
            super.pullingPercent = newValue
            
            let images: Array<UIImage> = stateImages[.idle]!
            if state != .idle || images.count == 0 {
                return
            }
            gifView.stopAnimating()
            var index: Int = images.count * Int(newValue!)
            if index >= images.count {
                index = images.count - 1
            }
            gifView.image = images[index]
        }
        
        get {
            return _pullingPercent
        }
    }
    override var state: MJRefreshComponent.MJRefreshState {
        set {
            if super.state != newValue {
                super.state = newValue
            }
            
            if state == .pulling || state == .refreshing {
                let images: Array<UIImage> = stateImages[state]!
                if images.count == 0 {
                    return
                }
                
                gifView.isHidden = false
                gifView.stopAnimating()
                if images.count == 1 {
                    gifView.image = images.last!
                } else {
                    gifView.animationImages = images
                    gifView.animationDuration = stateDurations[state]!
                    gifView.startAnimating()
                }
            } else if state == .idle {
                gifView.isHidden = false
            } else if state == .noMoreData {
                gifView.isHidden = true
            }
        }
        
        get {
            return _state
        }
    }
    override init(refreshingClosure: @escaping MJRefreshComponentRefreshingClosure) {
        super.init(refreshingClosure: refreshingClosure)
        
        addSubview(gifView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setImages(_ images: Array<UIImage>, duration: TimeInterval, forState state: MJRefreshState) -> Void {
        stateImages[state] = images
        stateDurations[state] = duration
        
        let image: UIImage = images.first!
        if image.size.height > mj_h {
            mj_h = image.size.height
        }
    }
    
    func setImages(_ images: Array<UIImage>, forState state: MJRefreshState) -> Void {
        setImages(images, duration: TimeInterval(images.count) * 0.1, forState: state)
    }
    
    override func prepare() {
        super.prepare()
        
        labelLeftInset = 20
    }
    
    override func placeSubviews() {
        super.placeSubviews()
        
        if gifView.constraints.count > 0 {
            return
        }
        
        gifView.frame = bounds;
        if stateLabel.isHidden {
            gifView.contentMode = .center
        } else {
            gifView.contentMode = .right
            gifView.mj_w = mj_w * 0.5 - labelLeftInset - stateLabel.mj_textWith * 0.5
        }
    }
    

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
